<template>
    <div class="app-container">
        <el-card class="box-card">
            <div class="flex justify-between">
                <el-button type="primary" size="default" @click="handleAdd">新增</el-button>
                <!-- <div class="flex justify-end">
                    <div class="flex items-center mr-4">
                        <span class="item">奖品名称：</span>
                        <el-input v-model="listQuery.title" placeholder="请输入想要搜索的奖品名称" size="normal" clearable></el-input>
                    </div>
                    <div class="flex items-center mr-4">
                        <span class="">奖品标签：</span>
                        <el-select v-model="listQuery.label_id" value-key="" placeholder="请选择" clearable filterable>
                            <el-option v-for="item in options"
                                :key="item.id"
                                :label="item.title"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </div>
                    <el-button type="primary" size="default" @click="getTableData">搜索</el-button>
                    <el-button type="primary" size="default" @click="handleReset">重置</el-button>
                </div> -->
            </div>
            <el-table
                ref="multipleTable"
                v-loading="loading"
                :data="tableData"
                border
                class="mt-3"
                stripe
                style="width: 100%"
                tooltip-effect="dark"
            >
                <el-table-column label="序号" type="index" :index="indexMethod" prop="user_code" width="100px"></el-table-column>
                <el-table-column label="萌宠蛋ID" prop="id"></el-table-column>
                <el-table-column label="萌宠蛋名称" prop="name"></el-table-column>
                <el-table-column label="萌宠蛋图片" prop="icon">
                    <template slot-scope="{row}">
                        <el-image v-if="row.icon" :src="row.icon" class="imgaes" fit="fill" :lazy="true"></el-image>
                    </template>
                </el-table-column>
                <!-- <el-table-column label="孵化动效" prop="born_special">
                    <template slot-scope="{row}">
                        <el-image v-if="!row.born_special.includes('.mp4')" :src="row.born_special" class="imgaes" fit="fill" :lazy="true"></el-image>
                        <video
                            v-else
                            :src="row.born_special"
                            autoplay 
                            controls
                            muted
                            class="imgaes"
                        ></video>
                    </template>
                </el-table-column> -->
                <el-table-column label="收费方式" prop="type">
                    <template slot-scope="scoped">
                        <el-tag v-if="scoped.row.type === 'default'" type="success">免费</el-tag>
                        <el-tag v-if="scoped.row.type === 'special'" type="danger">收费</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="价值（M豆）" prop="coin"></el-table-column>
                <el-table-column label="状态" prop="is_show">
                    <template slot-scope="scoped">
                        <el-tag v-if="scoped.row.is_show === 1" type="success">上架</el-tag>
                        <el-tag v-if="scoped.row.is_show === 0" type="danger">下架</el-tag>
                    </template>
                </el-table-column>
                <!-- <el-table-column label="萌宠名称" prop="pet_name"></el-table-column> -->
                <el-table-column label="进化金币" prop="evolve_coin"></el-table-column>
                <el-table-column label="创建时间" prop="created_at"></el-table-column>
                <el-table-column label="萌宠蛋说明" prop="desc"></el-table-column>
                
                <el-table-column label="操作" width="250px">
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            @click="handleEdit(scope.row)"
                        >编辑
                        </el-button>
                        <el-button
                            v-if="scope.row.is_show === 0"
                            size="mini"
                            type="text"
                            @click="handleStatus(scope.row,1)"
                        >上架
                        </el-button>
                        <el-button
                            v-if="scope.row.is_show === 1"
                            size="mini"
                            type="text"
                            @click="handleStatus(scope.row,2)"
                        >下架
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
    
            <!-- 分页 -->
            <cus-pagination
                v-show="total > 0"
                :limit.sync="listQuery.perPage"
                :page.sync="listQuery.page"
                :total="total"
                @pagination="getTableData"
            />
        </el-card>
  
        <addEditModal
            :dialogVisible.sync="dialogVisible"
            :title.sync="dialogTitle"
            :info="info"
            @updateTable="getTableData"
        ></addEditModal>
    </div>
</template>
  
<script>
    import { petEggIsShow, petEggList } from '@/api/petsManagement'
    import addEditModal from './childComps/eggAddEditModal'
  	export default {
        name: 'petEggList',
        components: {addEditModal},
        data() {
            return {
                dialogTitle: 'add',
                dialogVisible: false,
                info: {},
                loading: false,
                tableData: [],
                listQuery: {
                    page: 1,
                    perPage: 10
                },
                total: 1,
                options: []
            }
        },
        created() {
            this.getTableData()
        },
        methods: {
            indexMethod(index) {
                return this.total - (this.listQuery.page-1) * this.listQuery.perPage - index
            },
            updateTable(data) {
                console.log(data)
                
                this.updateInfo = {
                    id: data.id,
                    name: data.name,
                    pet_name: data.pet_name,
                    born_special: data.born_special,
                    coin: data.coin,
                    evolve_coin: data.evolve_coin,
                    second: data.second,
                    type: data.type,
                    rate: data.rate,
                    desc: data.desc,
                    icon: data.icon,
                    banner_color: {
                        default: data.banner_color.default,
                        dynamic: data.banner_color.default,
                    }
                }
            },
            // 网络请求
            /**
             * getTableData
             * @desc 获取表格数据
             */
            async getTableData() {
                this.loading = true
                for (let key in this.listQuery) {
                    this.listQuery[key] = this.listQuery[key] === '' ? undefined : this.listQuery[key]
                }
                const {data, code} = await petEggList(this.listQuery)
                if (code === 200) {
                    this.tableData = data.data
                    this.total = data.total
                }
                this.loading = false
        
            },
            closeDialog(form) {
                if (this.$refs[form]) {
                    this.$refs[form].clearValidate() // 清除整个表单的校验
                    this.$refs[form].resetFields() // 清除整个表单的校验
                }
            },
            handleEdit(row) {
                this.dialogVisible = true
                this.dialogTitle = 'edit'
                this.info = row
            },
            handleStatus(row, type) {
                if(type == 1) {
                    this.$confirm('确定上架该奖励？上架后将显示在客户端', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        petEggIsShow({
                            id: row.id,
                            is_show: 1
                        }).then(res => {
                            if(res.code === 200) {
                                this.$message({
                                    type: 'success',
                                    message: '操作成功'
                                })
                            }
                            this.dialogVisible = false
                            this.getTableData()
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                }
                if(type == 2) {
                    this.$confirm('确定下架该奖励？下架后将不显示在客户端', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        petEggIsShow({
                            id: row.id,
                            is_show: 0
                        }).then(res => {
                            if(res.code === 200) {
                                this.$message({
                                    type: 'success',
                                    message: '操作成功'
                                })
                            }
                            this.dialogVisible = false
                            this.getTableData()
                        })
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消下架'
                        });
                    });
                }
                
            },
            handleSDelete(row) {
                this.$confirm('确定删除该奖励？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    wishProductDel({
                        id: row.id
                    }).then(res => {
                        if(res.code === 200) {
                            this.$message({
                                type: 'success',
                                message: '操作成功'
                            })
                            this.getTableData()
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleReset() {
                this.listQuery = {
                    page: 1,
                    perPage: 10
                }
                this.getTableData()
            },
            handleAdd() {
                this.dialogVisible = true
                this.dialogTitle = 'add'
            }
        }
    }
</script>
  
<style lang="scss" scoped>
.infoBox {
  margin-top: 15px;
  margin-bottom: 15px;
  padding-bottom: 15px;
  border: 1px solid #999999;
  border-radius: 10px;
  display: flex;
  align-items: baseline;
  justify-content: center;
  box-sizing: border-box;
  padding-right: 30px;
  .info_1{
    display: flex;
    justify-content: center;
    flex-direction: column;
    width: 100%;

    .info_2{
      margin-top: 15px;
      display: flex;
      align-items: center;
	  position: relative;

      span{
        width: 180px;
        text-align: right;
      }
	  .tip{
			color: red;
			font-size: 12px;
			position: absolute;
			top: 100%;
			left: 120px;
		}
    }
  }
}
.item {
    width: 100px;
}
.formItemBox{
	display: flex;
	align-items: center;
	flex-wrap: wrap;

	width: 100%;
	overflow: hidden;
}
::v-deep {
	.formItemBox{
		.el-form-item {
			width: 50% !important;
		}
	}
    
}

.imgaes{
    width: 100px;
}

</style>
  